package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.OrderBill;
import cn.wolfcode.crm.domain.ProductSortTwo;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IOrderBillService;
import cn.wolfcode.crm.service.ISupplierService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.qo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("orderBill")
public class OrderBillController {
    @Autowired
    private IOrderBillService orderBillService;

    //仅用以转发到orderBill.jsp界面
    @RequestMapping("view")
    public String view() {
        return "/orderBill/orderBill";
    }

    @RequestMapping("query")
    @ResponseBody
    public Object query(QueryObject qo){
        PageResult result = orderBillService.query(qo);
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        if (Optional.ofNullable(id).isPresent()) {
            orderBillService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(OrderBill entity){
        orderBillService.saveOrUpdate(entity);
        return new JSONResult();
    }

    //单据的审核操作
    @RequestMapping("audit")
    @ResponseBody
    public Object audit(Long id) {
        if (Optional.ofNullable(id).isPresent()) {
            orderBillService.audit(id);
        }
        return new JSONResult();
    }

    //通过sn查询
    @RequestMapping("getSn")
    @ResponseBody
    public Object getSn(String sn) {
        //将数据转换成json格式
        List<OrderBill> orderBills = orderBillService.selectBySn(sn);
        return orderBills;
    }
}
